/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.entity.WebServicesMethodWrapper;
import han.bifang.gwt.client.common.entity.WsdlWrapper;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class ChooseMethodPanel extends Composite {
	private WsdlWrapper w;
	private VerticalPanel v ;
	private ServiceMessages cons;
	private List oldMethods;
	
	
	
	private HashMap tmp = new HashMap();
	
	public WsdlWrapper getWsdlWrapper(){
		ArrayList chooseMethods = new ArrayList();
		for(int i = 0 , j = oldMethods.size();  i <  j  ; i ++){
			WebServicesMethodWrapper wb = (WebServicesMethodWrapper)oldMethods.get(i);
			if(tmp.get(wb.getMethod().getMethodName()) != null){
				chooseMethods.add(wb);
			}			
		}
		
		w.setMethods(chooseMethods);
		return w;
		
	}
	

	public ChooseMethodPanel(ServiceMessages cons,
			WsdlWrapper w) {
		this.w = w;
		this.cons = cons;
		oldMethods = w.getMethods();
		v = new VerticalPanel();
		v.setWidth("100%");
		v.setSpacing(5);

		v.add(new HTML(cons.chooseMethod()));
		
		Grid table = new Grid(w.getMethods().size() + 1, 1);
		table.setWidth("100%");
		table.setBorderWidth(1);
		table.setCellPadding(1);
		table.setCellSpacing(1);
		
		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "45%");
		//cf.setWidth(1, "75%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		//ff.setStyleName(0, 1, "tablecol");
		
		table.setHTML(0, 0, cons.methodName());
		//table.setHTML(0, 1, cons.methodDetail());
		
		for (int i = 1, j = oldMethods.size(); i <= j; i++) {
			WebServicesMethodWrapper wb = (WebServicesMethodWrapper)oldMethods.get(i - 1);
			CheckBox b = new CheckBox(wb.getMethod().getMethodName());
			b.addClickListener(new MethodClickListener(tmp) );
			
			table.setWidget(i ,0, b);
			//table.setWidget(i ,1, new MethodDetailPanel(cons,wb));		
			
		}
		v.add(table);
		
		initWidget(v);
	}
	
	
	
	private static class MethodClickListener implements ClickListener{
		private HashMap t;
		public MethodClickListener(HashMap t){
			this.t = t;
	
		}
		public void onClick(Widget sender) {
			CheckBox b = (CheckBox)sender;
			if(b.isChecked()){
				//Window.alert("set");
				t.put(b.getText(), b.getText());
			}else{
				t.remove(b.getText());
			}
		}
	}

}
